package com.southminority.ethnic.controller.coures;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.coures.vo.CourseMaterialReqVo;
import com.southminority.ethnic.controller.coures.vo.CourseMaterialVo;
import com.southminority.ethnic.service.course.CourseMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 课程资料Controller
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "课程资料", description = "课程资料相关接口")
@RestController
@RequestMapping("/api/course/material")
@CrossOrigin
@Validated
public class CourseMaterialController {

    @Autowired
    private CourseMaterialService courseMaterialService;

    /**
     * 根据课程ID获取课程资料列表
     */
    @Operation(summary = "根据课程ID获取课程资料列表")
    @PostMapping("/list")
    public R<List<CourseMaterialVo>> getCourseMaterials(@Valid @RequestBody CourseMaterialReqVo reqVo) {
        try {
            List<CourseMaterialVo> materials = courseMaterialService.getMaterialsByCourseId(reqVo.getCourseId());
            
            log.info("获取课程资料列表成功，课程ID: {}, 资料数量: {}", reqVo.getCourseId(), materials.size());
            return R.ok(materials);
            
        } catch (Exception e) {
            log.error("获取课程资料列表失败，课程ID: {}", reqVo.getCourseId(), e);
            return R.fail("获取课程资料失败：" + e.getMessage());
        }
    }

    /**
     * 根据课程ID获取课程资料列表（GET方式）
     */
    @Operation(summary = "根据课程ID获取课程资料列表（GET方式）")
    @GetMapping("/list/{courseId}")
    public R<List<CourseMaterialVo>> getCourseMaterialsByGet(@PathVariable @NotNull Long courseId) {
        try {
            List<CourseMaterialVo> materials = courseMaterialService.getMaterialsByCourseId(courseId);
            
            log.info("获取课程资料列表成功，课程ID: {}, 资料数量: {}", courseId, materials.size());
            return R.ok(materials);
            
        } catch (Exception e) {
            log.error("获取课程资料列表失败，课程ID: {}", courseId, e);
            return R.fail("获取课程资料失败：" + e.getMessage());
        }
    }

    /**
     * 增加资料下载次数
     */
    @Operation(summary = "增加资料下载次数")
    @PostMapping("/download/{materialId}")
    public R<Boolean> increaseDownloadCount(@PathVariable @NotNull Long materialId) {
        try {
            Boolean success = courseMaterialService.increaseDownloadCount(materialId);
            
            if (success) {
                log.info("增加资料下载次数成功，资料ID: {}", materialId);
                return R.ok(true);
            } else {
                log.warn("增加资料下载次数失败，资料ID: {}", materialId);
                return R.fail("增加下载次数失败，可能资料不存在");
            }
            
        } catch (Exception e) {
            log.error("增加资料下载次数失败，资料ID: {}", materialId, e);
            return R.fail("增加下载次数失败：" + e.getMessage());
        }
    }
} 